from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
import os

import models, forms

version = 2
class MainPage(webapp.RequestHandler):      
    def get(self):
        self.response.out.write(template.render("templates/blog/index.html", {"version":version,}))
        
class AboutMe(webapp.RequestHandler):
    def get(self):
        self.response.out.write(template.render("templates/blog/aboutme.html", {"version":version,}))
        
class notes_view(webapp.RequestHandler):
    def get(self):
        notes = models.Note.all().order("-datetime")
        form = forms.notes_add_form()
        self.response.out.write(template.render("templates/blog/notes_view.html",
            {"form":form,"notes":notes,"version":version,}))
    def post(self):

        form =forms.notes_add_form(self.request)
        if form.is_valid():
            data = form.save(commit=False)
            data.ipaddress = os.environ["REMOTE_ADDR"]
            data.save()
            self.redirect(os.environ["HTTP_REFERER"])
        else:
            notes = models.Note.all().order("-datetime")
            self.response.out.write(template.render("templates/blog/notes_view.html",
                {"form":form,"notes":notes,"version":version,}))
        
